package com.cashmama.market.api.application.controller.order;

import com.cashmama.market.api.application.annos.SignIgnore;
import com.cashmama.market.api.application.annos.VisitorAccessible;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.utils.StringUtils;
import com.fqgj.xjd.user.client.UserService;
import com.fqgj.xjd.user.client.response.UserProductPromotionLogRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/12/2
 * Time: 9:51 上午
 */
@Controller
@RequestMapping("/jp")
public class JumpController {

    private static final Logger logger = LoggerFactory.getLogger(JumpController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/go")
    public String jump(String target) {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        UserProductPromotionLogRes userProductPromotionLogRes = new UserProductPromotionLogRes()
                .setActionFrom(userCode)
                .setActionTarget(target)
                .setActionType("app click");
        userService.addPromotion(userProductPromotionLogRes);
        return "redirect:" + target;
    }

    @SignIgnore
    @VisitorAccessible
    @RequestMapping("/go1")
    public String jump1(String target, String userCode, HttpServletRequest request) {
        String from = getIpAddr(request);
        if (!StringUtils.isEmpty(userCode)) {
            from = userCode;
        }
        UserProductPromotionLogRes userProductPromotionLogRes = new UserProductPromotionLogRes()
                .setActionFrom(from)
                .setActionTarget(target)
                .setActionType("app click");
        userService.addPromotion(userProductPromotionLogRes);
        return "redirect:" + target;
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = "no";
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        // ipAddress = this.getRequest().getRemoteAddr();
        logger.info("== 获取到请求的ip地址为 ==,ipAddress:{}", ipAddress);
        return ipAddress;
    }


}
